package gamedemo.manager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

import gamedemo.entity.Entity;
import gamedemo.entity.EntityZComparator;

/**
 * @author thi
 * 
 *         Håller koll på alla aktiva entities i spelet.
 * 
 *         Entities ska renderas i ordning efter deras värde på position.z.
 */
public class EntityManager {
	private ArrayList<Entity> entityList;
	public ArrayList<Entity> getEntityList() {
		return entityList;
	}

	private Comparator<Entity> comparator;

	public EntityManager() {
		entityList = new ArrayList<Entity>();
		comparator = new EntityZComparator();
	}

	private void sort() {
		Collections.sort(entityList, comparator);
	}

	public void add(Entity e) {
		entityList.add(e);
	}

	public void update(GameContainer gc, StateBasedGame sb, int delta)
			throws SlickException {
		/*for (Entity e : entityList) {
			e.update(gc, sb, delta);
		}*/
		for(int i = 0; i < entityList.size(); i++) {
			entityList.get(i).update(gc, sb, delta);
		}

		sort();
	}

	public void render(GameContainer gc, StateBasedGame sb, Graphics gr)
			throws SlickException {
		for (Entity e : entityList) {
			e.render(gc, sb, gr);
		}
	}
}
